package com.meme.editor.model.sprite2d;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ListIterator;


import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;

import com.meme.editor.property.PropertyFactory;
import com.meme.editor.property.value.NumberPropertyValue;

import com.meme.util.ObjectRef;

import org.apache.log4j.Logger;

public class PureModule extends Module
{
	static Logger logger = Logger.getLogger (PureModule.class);
	
	public final static String GroupName_PureImageModule = "Pure Module";
	
	public final static String PropertyName_ModuleX = "X"; 
	public final static String PropertyName_ModuleY = "Y"; 
	public final static String PropertyName_ModuleW = "Width"; 
	public final static String PropertyName_ModuleH = "Height"; 
	
	int mStartX;
	int mStartY;
	int mWidth;
	int mHeight;	

	public PureModule (int x, int y, int w, int h)
	{
		this (null, x, y, w, h);
	}
	
	public PureModule (String name, int x, int y, int w, int h)
	{
		this (name, null, x, y, w, h);
	}
	

	public PureModule (String name, ModuleGroup group, int x, int y, int w, int h)
	{
		super (name, group);		
		
		createPublicProperty (GroupName_PureImageModule, PropertyName_ModuleX, PropertyFactory.ValueType_Number );
		createPublicProperty (GroupName_PureImageModule, PropertyName_ModuleY, PropertyFactory.ValueType_Number );
		createPublicProperty (GroupName_PureImageModule, PropertyName_ModuleW, PropertyFactory.ValueType_Number );
		createPublicProperty (GroupName_PureImageModule, PropertyName_ModuleH, PropertyFactory.ValueType_Number );

		setProperties (x, y, w, h);
	}
	


	

	
	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{		
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if (   mod.mGroupName.equals (GroupName_PureImageModule) )
			{
				int value = ( (Float) (mod.mNewValue) ).intValue();
				
				if ( mod.mPropertyName.equals (PropertyName_ModuleX) )
				{
					setX (value);
					iter.remove();
				}
				else if ( mod.mPropertyName.equals (PropertyName_ModuleY) )
				{
					setY (value);
					iter.remove();
				}
				else if ( mod.mPropertyName.equals (PropertyName_ModuleW) )
				{
					setWidth (value);
					iter.remove();
				}
				else if ( mod.mPropertyName.equals (PropertyName_ModuleH) )
				{
					setHeight (value);
					iter.remove();
				}
			}
		}
			
		super._handleModifyingPropertyValues (modications);
	}	
	
	public int getX ()
	{
		return mStartX;		
	}
	
	public int getY ()
	{
		return mStartY;
	}
	
	public void setX (int x)
	{
		mStartX = x;
		changePublicPropertyValue ( GroupName_PureImageModule, PropertyName_ModuleX, new Float (mStartX) );
	}
	
	public void setY (int y)
	{
		mStartY = y;
		changePublicPropertyValue ( GroupName_PureImageModule, PropertyName_ModuleY, new Float (mStartY) );
	}
	
	public void setWidth (int w)
	{
		mWidth  = w;
		changePublicPropertyValue ( GroupName_PureImageModule, PropertyName_ModuleW, new Float (mWidth) );
	}
	
	public void setHeight (int h)
	{
		mHeight = h;
		changePublicPropertyValue ( GroupName_PureImageModule, PropertyName_ModuleH, new Float (mHeight) );
	}
	
	public void setProperties (int x, int y, int w, int h)
	{
		setX (x);
		setY (y);
		setWidth  (w);
		setHeight (h);		
	}
	
	@Override
	public boolean isValid ()
	{
		if ( mStartX < 0 || mStartY < 0 || mWidth < 1 || mHeight < 1 )
			return false;

		PureModuleGroup imgModuleGroup = (PureModuleGroup) getParent ();
		
		if ( ! imgModuleGroup.isValid() )
			return false;
		
		ModuleImage moduleImage = imgModuleGroup.getModuleImage();
		
		if ( ! moduleImage.isValid() )
			return false;
		
		if ( mStartX + mWidth > moduleImage.getWidth()
				|| mStartY + mHeight > moduleImage.getHeight() )
			return false;
		
		return true;
	}
	
	@Override
	public int getMinX ()
	{
		return 0;
	}
	
//	@Override
//	public int getMaxX ()
//	{
//		return 0;
//	}
	
	@Override
	public int getMinY ()
	{
		return 0;
	}
	
//	@Override
//	public int getMaxY ()
//	{
//		return mHeight;
//	}
	
	@Override
	public int getWidth ()
	{
		return mWidth;
	}
	
	@Override
	public int getHeight ()
	{
		return mHeight;
	}
	
	public Rectangle getBounds ()
	{
		return new Rectangle (0, 0, mWidth, mHeight);
	}
	
	@Override
	public boolean isImageHybird ()
	{
		return false;
	}

	@Override
	public boolean isPaletteHybird ()
	{
		return false;
	}
	
	@Override
	public void render (Graphics2D g, int posX, int posY, boolean flipX, boolean flipY, int scale, int flags)
	{
		if ( ! isValid () )
			return;
		
		g.drawImage (
				( (PureModuleGroup) getParent () ).getModuleImage().getImage(),
				posX, posY, posX + mWidth * scale * (flipX ? -1 : 1), posY + mHeight * scale * (flipY ? - 1 : 1),
				mStartX, mStartY, mStartX + mWidth, mStartY + mHeight,
				null
			);
	}
	
}
